#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename	:  01-redis.sh
# Version	:  1.0
# Date		:  2024/10/16
# Author	:  yaoxiyao
# Email		:  yaoxiyao@kylinos.com.cn
# History	:
#              Version 1.0, 2024/10/16
# Function	:  redis
# Out		:
#              0 => TPASS
#              1 => TFAIL
#              other=> TCONF
# ----------------------------------------------------------------------

# 测试主题
Title_Env_LTFLIB="redis"

## TODO : 个性化,初始化
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestInit_LTFLIB() {
    return $TPASS
}

## TODO : 清理函数
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
TestClean_LTFLIB() {
    return $TPASS
}

## TODO : 测试用例集
#   Out : 0=>TPASS
#         1=>TFAIL
#         2=>TCONF
Testsuite_LTFLIB() {
    testcase_1
    return $TPASS
}

## TODO ： 支持dmesg日志查看
testcase_1() {
    rpm -qa | grep redis
    CommRetParse_LTFLIB "rpm -qa|grep redis"
    systemctl start redis
    CommRetParse_LTFLIB "systemctl start redis"
    systemctl status redis | grep "active (running)"
    CommRetParse_LTFLIB "systemctl status redis | grep active (running)"

    # 测试连接到 Redis 服务器
    redis-cli PING >/dev/null

    if [ $? -eq 0 ]; then
        echo "Redis 服务器可用！"

        # 设置一个键值对
        echo "设置键 mykey 的值为 'Hello, Redis!'"
        redis-cli SET mykey "Hello, Redis!"

        # 获取该键的值
        value=$(redis-cli GET mykey)

        if [ "$value" == "Hello, Redis!" ]; then
            echo "测试成功！获取到的值为: $value"
        else
            echo "测试失败！获取到的值不正确: $value"
        fi

    else
        echo "无法连接到 Redis 服务器，请检查服务器状态。"
    fi
}

#----------------------------------------------#

source "${LIB_LTFLIB}"
Main_LTFLIB $@
